package com.soft.dao;


import com.soft.entity.Profile;
import com.soft.util.DBHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//公司简介
public class ProfileDAO {
    public Profile getAll() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBHelper.getConnection();
            String sql = "select * from company_profile;";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            if (rs.next()) {
                Profile lists = new Profile();
                lists.setId(rs.getInt("id"));
                lists.setContent(rs.getString("content"));
                return lists;
            }
            return null;

        }
        catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {

            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }


    public void add(Profile profile) {
        String sql = "insert into company_profile(content) values(?);";
        PreparedStatement pstmt = null;
        try {
            pstmt = DBHelper.getConnection().prepareStatement(sql);
            pstmt.setString(1, profile.getContent());
            pstmt.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public void delete(Profile profile) {
        String sql = "delete from company_profile where id=?;";
        PreparedStatement pstmt = null;
        try {
            pstmt = DBHelper.getConnection().prepareStatement(sql);
            pstmt.setInt(1, profile.getId());
            pstmt.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }



    public Profile getone(Profile profile) {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            String sql = "select * from company_profile where id=? ;";
            stmt = DBHelper.getConnection().prepareStatement(sql);
            stmt.setInt(1, profile.getId());
            rs = stmt.executeQuery();
            if (rs.next()) {
                Profile lists = new Profile();
                lists.setId(rs.getInt("id"));
                lists.setContent(rs.getString("content"));
                return lists;
            }
            return null;
        }
        catch (Exception ex) {
            ex.printStackTrace();
        } finally {

            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return null;
    }


    public void update(Profile profile) {
        String sql = "update company_profile set content=? where id=? ;";
        PreparedStatement pstmt = null;
        try {
            pstmt = DBHelper.getConnection().prepareStatement(sql);
            pstmt.setString(1, profile.getContent());
            pstmt.setInt(2, profile.getId());
            pstmt.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
